package com.ruoyi.common.utils;


import cn.hutool.core.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;

public class AesUtils {

    public static String decrypt(String sessionKey,String encryptedIv,String encryptedData) throws Exception{
        // 转化为字节数组
        byte[] key = Base64.decode(sessionKey);
        byte[] iv = Base64.decode(encryptedIv);
        byte[] encData = Base64.decode(encryptedData);
        // 假如密钥缺乏16位，那么就补足
        int base =16;
        if (key.length % base !=0) {
            int groups = key.length / base +(key.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp,(byte) 0);
            System.arraycopy(key,0,temp,0,key.length);
            key = temp;
        }
        // 假如初始向量缺乏16位，也补足
        if (iv.length % base !=0) {
            int groups = iv.length / base +(iv.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp,(byte) 0);
            System.arraycopy(iv,0,temp,0,iv.length);
            iv = temp;
        }
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
        String resultStr = null;
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
            cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
            resultStr = new String(cipher.doFinal(encData),"UTF-8");
        } catch (Exception e){
            //            logger.info("解析错误");
            e.printStackTrace();
        }
        // 解析加密后的字符串
        return resultStr;
    }



    //    public static final String KEY = "qxhzngy266a186ke";//密钥key
    public static final String CWAY = "qxhzngy266a186ke";//密钥key
    public static final String IV = "1474568913573216";//向量iv

    //加密
    public static String encrypt(String content) throws Exception {
        byte[] raw = CWAY.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
        //使用CBC模式，需要一个向量iv，可增加加密算法的强度
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        //return new BASE64Encoder().encode(encrypted);
        return org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted);
    }

    //解密
    public static String decrypt(String content) throws Exception {
        try {
            byte[] raw = CWAY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
            //byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
            byte[] encrypted1 = org.apache.commons.codec.binary.Base64.decodeBase64(content);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args){

        String key = null;
        try {
            key = AesUtils.encrypt("发给你看是大法官考试大纲机构多了哥斯达黎加公里数对接改了结束了");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(key);

        key = "vIFMt4e7dhG3U4Rw4OXbi8/DM8c+or8EsTI88GfoLuszmfny2Z77nNHwOMatLcDvZ95IQimWK/jLGxrvUTn5wt/D/MVwJ4p756svU5XRQ0i2l1aAb7KN+Eu9LVxN/0LAlgI4vOgSxNjWghLCrKiW3A==";

        String decrypt = null;
        try {
            decrypt = AesUtils.decrypt(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(decrypt);
    }

}
